<?php
require_once('ModelConfig.php');
	class db{
		public $conn = NULL;
		public $result = NULL;
		public $host = _host;
		public $user = _user;
		public $pass = _pass;
		public $database = _database;
		private $author="ThaiPhamDuy";
		protected $version="4.1";
		
		//định nghĩa hàm construct
		function __construct()
		{
			$this->conn = mysql_connect($this->host, $this->user, $this->pass);
			mysql_select_db($this -> database, $this->conn);
			mysql_query("set names 'utf8'"); 	 
			}
		 
		// định nghĩa hàm getdata
		public function Execute($sql) 
		{
		   $this->result = mysql_query($sql)  or die(mysql_error());
		   return $this->result;
		}
		
		//định nghĩa hàm fetchrow
		public function fetchRow($sql) 
		{
			if (is_resource($this->result) ==false) 
				return false;
		   	$row = mysql_fetch_assoc($this->result);
			return $row;	
		}
/*		
		
		//định nghĩa hàm thể loại
		public function TheLoai($lang='vi' , $AnHien=1){
		$sql="SELECT idTL, TenTL from theloai WHERE (AnHien=$AnHien or $AnHien=-1)
			AND (lang = '$lang' or '$lang'='') 
			ORDER BY ThuTu";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
		}//TheLoai
		//Định nghĩa hàm loại tin
		public function LoaiTin ($lang='vi',$AnHien=1){
		$sql="SELECT idLT, Ten  FROM loaitin WHERE (AnHien = $AnHien or $AnHien = -1) 
			AND (lang = '$lang' or '$lang'='') 
			ORDER BY Thutu";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
		} //LoaiTin	
		//Tin trong thể loại
		function LoaiTinTrongTheLoai($idTL,$AnHien=1){
			settype($idTL, "int");	
			$sql = "SELECT idLT, Ten FROM loaitin  WHERE (AnHien=$AnHien or $AnHien=-1)  
			  AND	idTL=$idTL ORDER BY ThuTu ASC";
			$kq = mysql_query($sql) or die(mysql_error());	
			return $kq;
		}//end LoaiTinTrongTheLoai
		function HangTrongCL($idCL,$AnHien=1){
			settype($idCL, "int");	
			$sql = "SELECT idHang, TenHang FROM hang WHERE (AnHien=$AnHien or $AnHien=-1)  
			  AND	idCL=$idCL ORDER BY ThuTu ASC";
			$kq = mysql_query($sql) or die(mysql_error());	
			return $kq;
		}//end LoaiTinTrongTheLoai
		//Định nghĩa hàm changeTitle
		function changeTitle($str){
		$str = $this->stripUnicode($str);
		$str = str_replace("?","",$str);
		$str = str_replace("&","",$str);
		$str = str_replace("'","",$str);		
		$str = str_replace("  "," ",$str);
		$str = trim($str);
		$str = mb_convert_case($str , MB_CASE_TITLE , 'utf-8');
	// MB_CASE_UPPER/MB_CASE_TITLE/MB_CASE_LOWER
		$str = str_replace(" ","-",$str);	
		return $str;
	}
	function stripUnicode($str){
		if(!$str) return false;
		$unicode = array(
		 'a'=>'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ',
		 'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
		 'd'=>'đ',
		 'D'=>'Đ',
		 'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
		 'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
		 'i'=>'í|ì|ỉ|ĩ|ị',	  
		 'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
		 'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
		 'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
		 'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
		 'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
		 'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
		 'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
		);
		foreach($unicode as $khongdau=>$codau) {
		  $arr = explode("|",$codau);
		  $str = str_replace($arr,$khongdau,$str);
		}
		return $str;
		}//end định nghĩa hàm changeTitle
		function DemSoTinTrong1Loai($idTL=-1, $idLT=-1, $lang='vi', $AnHien=-1){
		$sql="SELECT count(*) FROM  tin 
			  WHERE (idTL=$idTL OR $idTL=-1) 
			AND (idLT=$idLT OR $idLT=-1) 
			AND (lang='$lang' OR '$lang'='') 
			AND (AnHien=$AnHien OR $AnHien=-1) ";
		$rs = mysql_query($sql) or die(mysql_error());
		$rows_rs = mysql_fetch_row($rs);
		$totalRows = $rows_rs[0];
		return $totalRows;
		}//end đếm số dòng trong loại
		
		function DemSoSPTrong1Loai($idCL=-1, $idHang=-1, $AnHien=-1){
		$sql="SELECT count(*) FROM  sanpham 
			WHERE (idCL=$idCL OR $idCL=-1) 
			AND (idHang=$idHang OR $idHang=-1)  
			AND (AnHien=$AnHien OR $AnHien=-1) ";
		$rs = mysql_query($sql) or die(mysql_error());
		$rows_rs = mysql_fetch_row($rs);
		$totalRows = $rows_rs[0];
		return $totalRows;
		}//end đếm số dòng trong loại
		function DemSoLoaiTin($idLT=-1, $lang='vi', $AnHien=-1){
		$sql="SELECT count(*) FROM  loaitin ";
		$rs = mysql_query($sql) or die(mysql_error());
		$rows_rs = mysql_fetch_row($rs);
		$totalRows = $rows_rs[0];
		return $totalRows;
		}//end đếm số dòng trong loại
		function pagesLink($totalRows, $pageNum=1, $pageSize=5){	
			$baseURL = $_SERVER['PHP_SELF'];
			parse_str($_SERVER['QUERY_STRING'],$arr);
			unset($arr['pageNum']);		
			foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
			$baseURL .="?".$str;
						
			if ($totalRows<=0) return "";
			$totalPages = ceil($totalRows/$pageSize);
			if ($totalPages<=1) return "";
							
			$firstLink="";  $prevLink="";  $lastLink="";  $nextLink="";
			
			if ($pageNum > 1) {
				$firstLink = "<a href='$baseURL'>Trang đầu</a>";
				$prevPage = $pageNum - 1;
				$prevLink="<a href='$baseURL&pageNum=$prevPage'>Trang trước</a>";
			}
			if ($pageNum < $totalPages) { 
				$lastLink = "<a href='$baseURL&pageNum=$totalPages'>Trangcuối</a>";
				$nextPage = $pageNum + 1;
				$nextLink = "<a href='$baseURL&pageNum=$nextPage'>Trang kế</a>";
		} 
			return $firstLink.$prevLink.$nextLink.$lastLink;
		}//end pagelink
		function pagesList($totalRows , $pageNum=1, $pageSize = 5, $offset = 3){
		$baseURL = $_SERVER['PHP_SELF'];
		parse_str($_SERVER['QUERY_STRING'],$arr);
		unset($arr['pageNum']);		
		foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
		$baseURL .="?".$str;
		if ($totalRows<=0) return "";
		$totalPages = ceil($totalRows/$pageSize);
		if ($totalPages<=1) return "";
	
		$from = $pageNum - $offset;	
		$to = $pageNum + $offset;
		if ($from <=0) { $from = 1;   $to = $offset*2; }
		if ($to > $totalPages) { $to = $totalPages; }
		$links = "";
		for($j = $from; $j <= $to; $j++) {
			$links= $links . "<a href = '$baseURL&pageNum=$j'>$j</a>"; 
		} //for
		return $links;
	} // function pagesList
	function ChuoiNgauNhien($sokytu){
		$chuoi="ABCDEFGHIJKLMNOPQRSTUVWXYZWabcdefghijklmnopqrstuvwxyzw0123456789";
		for ($i=0; $i < $sokytu; $i++){
			$vitri = mt_rand( 0 ,strlen($chuoi) );
			$giatri= $giatri . substr($chuoi,$vitri,1 );
		}
		return $giatri;
	} 
		public function ChungLoai($AnHien=1){
		$sql="SELECT idCL, TenCL from chungloai WHERE (AnHien=$AnHien or $AnHien=2)			
			ORDER BY ThuTu";
		$kq = mysql_query($sql,$this->conn);
		return $kq;
	}//emnd chủng loại

	public function LoaiSP ($lang='vi',$AnHien=1){
		$sql="	SELECT idLoai, TenLoai  FROM loaisp
				WHERE (AnHien=$AnHien or $AnHien=2) 
				ORDER BY Thutu";
		$kq = mysql_query($sql,$this->conn) or die(mysql_error());	
		return $kq;
	} //Loaisam phẩm
	
	function LoaiSPTrongChungLoai($idCL,$AnHien=1){
		settype($idCL, "int");	
		$sql = "SELECT idLoai, TenLoai FROM loaisp  WHERE (AnHien=$AnHien or $AnHien=2)  
		  AND	idCL=$idCL ORDER BY ThuTu ASC";
		$kq = mysql_query($sql,$this->conn) or die(mysql_error());	
		return $kq;
	}
	function HangTrongChungLoai($idCL){
			settype($idCL, "int");	
			$sql = "SELECT idHang, TenHang FROM hang  WHERE	idCL=$idCL ORDER BY ThuTu ASC";
			$kq = mysql_query($sql) or die(mysql_error());	
			return $kq;
	}//end LoaiTinTrongChungLoai
	function User($idUser){
		settype($idUser, "int");	
		$sql = "SELECT idUser, HoTen,Username FROM users WHERE idUser=$idUser";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;
		
	}//end lấy thông tin user
	
		function TimKiemSanPham($tukhoa,$chungloai,$hang,$pageNum=1,$pageSize=10,$lang='vi', &$totalRows)
		{
			$tukhoa = trim(strip_tags($tukhoa));//trim, strip_tags từ khóa
			settype($idSP,"int");
			settype($chungloai,"int");//làm
			settype($hang,"int");
			$tukhoa = str_replace(array("?","+","*","(","[","|","\\"),"Từ khóa",$tukhoa);// thay thế ký tự đặc biệt thành từ khóa
			if ($tukhoa=="\\") $tukhoa='Từ khóa';
			if ($tukhoa=='') $tukhoa='Từ khóa';
			if (get_magic_quotes_gpc()==false) {
				$tukhoa = mysql_real_escape_string($tukhoa);		
			}
			 $totalRows = 0;
			//tính tổng số dòng
			$sql = "SELECT count(*) 
					FROM sanpham, chungloai, hang
					WHERE sanpham.AnHien = 1 
					
					AND sanpham.idCL = chungloai.idCL 
					AND sanpham.idHang= hang.idHang
					
					AND (TenSP RegExp '$tukhoa' or MoTa RegExp '$tukhoa')  
					 AND(sanpham.idCL = $chungloai or sanpham.idCL=0) 
					AND(sanpham.idCL = $chungloai or $chungloai=0) 
					AND(sanpham.idHang = $hang or sanpham.idHang=0)
	
					AND (sanpham.Lang='$lang' or '$lang'='')
					ORDER BY sanpham.idSP DESC";
			$kq = mysql_query($sql,$this->conn) or die (mysql_error());	
			$row_kq = mysql_fetch_row($kq);
			$totalRows = $row_kq[0];
			$startRow = ($pageNum - 1)*$pageSize;
			
			$sql = "SELECT idSP, TenSP, MoTa, UrlHinh,sanpham.idCL,sanpham.idHang
					FROM sanpham, chungloai, hang
					WHERE sanpham.AnHien = 1 
					
					AND sanpham.idCL = chungloai.idCL 
					AND sanpham.idHang= hang.idHang
					
					AND (TenSP RegExp '$tukhoa' or MoTa RegExp '$tukhoa')  
					 
					AND(sanpham.idCL = $chungloai or sanpham.idCL=0) 
					AND(sanpham.idHang = $hang or sanpham.idHang=0)
	
					AND (sanpham.Lang='$lang' or '$lang'='')
					ORDER BY sanpham.idSP DESC
					LIMIT $startRow,$totalRows";		
			$kq = mysql_query($sql) or die (mysql_error());
			return $kq;
		}//ket qua tim kiem sản phẩm
		function ListYKienTin($idTin){
		$sql="SELECT *
				FROM ykienkhachhang
				WHERE idTin=$idTin";
				$kq = mysql_query($sql) or die (mysql_error());
			return $kq;
		}
		function ListYKienSP($idSP){
		$sql="SELECT *
				FROM ykienkhachhang
				WHERE idSP=$idSP";
				$kq = mysql_query($sql) or die (mysql_error());
			return $kq;
		}
		function ListYKien(){
		$sql="SELECT *
				FROM ykienkhachhang
				";
				$kq = mysql_query($sql) or die (mysql_error());
			return $kq;
		}
		function XoaYKienKH($idYKienKH){
			settype($idYKienKH,"int");
				if ($idYKienKH<=0) return; 
				$sql="DELETE FROM ykienkhachhang WHERE idYKienKH = $idYKienKH";
				mysql_query($sql) or die(mysql_error());	
		}
		function Yahoo($AnHien=1){
		$sql="SELECT ID, HoTen,Nickname,Icons,MoTa,SDT from yahoo WHERE (AnHien=$AnHien or $AnHien=-1)
			ORDER BY ThuTu";
		$kq = mysql_query($sql) or die(mysql_error());
			return $kq;
	}//Yahoo
	
	function LuotTruyCap()
	{
		$sql="SELECT * FROM luottruycap";
		$kq=mysql_query($sql) or die(mysql_errno());
		return $kq;
	}//end luot truy cap
	
	*/
	}//end class db
?>